﻿package com.sz.common;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
import com.ibatis.sqlmap.engine.scope.SessionScope;
import com.ibatis.sqlmap.engine.scope.StatementScope;

public class CommonDAO  extends SqlMapClientDaoSupport implements ICommonDAO{
	
	@SuppressWarnings("unchecked")
	public List queryForList(String sql) throws SQLException {
		return getSqlMapClient().queryForList("common_sqlmap.select_common", sql);
	}
	
	public Object queryForObject(String sql) throws SQLException {
		return getSqlMapClient().queryForObject("common_sqlmap.select_common", sql);
	}
	
	@SuppressWarnings("unchecked")
	public List getDicValueList(String dic_name) throws SQLException {
		return getSqlMapClient().queryForList("common_sqlmap.getDicValueList", dic_name);
	}
	
	public String getDynamicSql(String id, Object obj) throws SQLException {
		// 动态取配置的SQL
		SqlMapClientImpl  impl = (SqlMapClientImpl )getSqlMapClient();
		MappedStatement smt = impl.getMappedStatement("ticket_sqlmap.query_Ticket");
		Sql sql = smt.getSql();		
	
		SessionScope sessionScope = new SessionScope();      
		sessionScope.incrementRequestStackDepth();      
		StatementScope statementScope = new StatementScope(sessionScope);      
		smt.initRequest(statementScope);      
	
		return sql.getSql(statementScope, obj);
	}
	
	public Connection getConnection() throws SQLException {
		Connection conn1 = getSqlMapClient().getCurrentConnection();
		Connection conn2 = getSqlMapClient().getDataSource().getConnection();
		return conn2;
		
	}
}
